using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;


namespace RLSoft.SharePoint.FAQ
{
    [Description("FAQ Web Part")]
    public partial class FAQWebPart : System.Web.UI.UserControl, SmartPart.IAdvancedUserControl
    {
        protected Guid ListId;
        private const string featureId = "e2c77bcf-9912-4870-bd48-76f4ca577fce";

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);



            if (!string.IsNullOrEmpty(FAQListName))
            {
                try
                {
                    SPList list = SPContext.Current.Web.Lists[FAQListName];
                    ListId = list.ID;
                    DataTable tbItems = list.Items.GetDataTable();
                    rpFAQ.DataSource = tbItems;
                    rpFAQ.DataBind();
                    rpFAQSummary.DataSource = tbItems;
                    rpFAQSummary.DataBind();

                    
                    //foreach(SPListItem item in list.Items) {
                    //    HtmlTableRow r = new HtmlTableRow();
                    //    HtmlTableCell c = new HtmlTableCell();
                    //    HyperLink a = new HyperLink();
                    //    //LiteralControl l = new LiteralControl();
                    //    //l.Text = string.Format("Q: {0}", item.Title);
                    //    a.Text = string.Format("{0}", item.Title);
                    //    a.Attributes["href"] = string.Format("#Q{0}", item.ID);
                    //    //a.NavigateUrl = string.Format("#Q{0}", item.ID);
                    //    c.Controls.Add(a);
                    //    r.Cells.Add(c);
                    //    QuestionsSummary.Rows.Add(r);
                    //}

                    //TableRow row;
                    //TableCell cell;
                    //SPListItem item = list.Items[0];
                    //for (int i = 0; i < list.DefaultView.ViewFields.Count; i++)
                    //{

                    //    string fieldName = list.DefaultView.ViewFields[i];
                    //    SPField field = list.Fields.GetField(fieldName);

                    //    row = new TableRow();
                    //    cell = new TableCell();
                    //    cell.Text = field.Title;
                    //    row.Cells.Add(cell);

                    //    cell = new TableCell();

                    //    // Add a control from RH.SharePoint.SharePointWebControls 
                    //    Control cntrl = SharePointWebControls.GetSharePointControls(field, list, item, SPControlMode.Display);
                    //    // if the control is null (because it can not be rendered with a SharePoint Control) return 
                    //    if (cntrl == null) continue;

                    //    cell.Controls.Add(cntrl);
                    //    row.Cells.Add(cell);

                    //    cell = new TableCell();
                    //    cell.Controls.Add(SharePointWebControls.GetSharePointControls(field, list, item, SPControlMode.Edit));
                    //    row.Controls.Add(cell);
                    //    Table1.Rows.Add(row);

                    //}

                    



                }
                catch (Exception ex)
                {

                    //throw;
                }

            }

        }

        //private string m_FAQListName;
        [Browsable(false), Category("Configuration"),
     Description("FAQ List Name"),
     DisplayName("FAQ List Name"), WebPartStorage(Storage.Shared)]
        public string FAQListName
        {
            get
            {
                if (this.curSmartPart.GetCustomProperty("FAQListName") != null)
                    return this.curSmartPart.GetCustomProperty("FAQListName").ToString();
                else
                    return
                    String.Empty;
            }
            set
            {
                this.curSmartPart.SetCustomProperty("FAQListName", value);
            }
        }


        #region IAdvancedUserControl Members

        public ToolPart[] GetCustomToolParts()
        {
            List<ToolPart> parts = new List<ToolPart>();
            GenericListSelectorToolPart picker = new GenericListSelectorToolPart(this, "FAQListName", new Guid(featureId));

            picker.Title = "Pick an FAQ List";
            parts.Add(picker);

            return parts.ToArray();
        }

        private SmartPart.SmartPart curSmartPart;
        public void SetContext(SmartPart.SmartPart webpart)
        {
            curSmartPart = webpart;
        }

        #endregion
    }
}